package com.listeningframework.boot.codegen.exception;

/**
 * 
 * @description Common Exceptions
 *
 * @author Listening
 * @version $Id: BusinessException.java, v 0.1 2016年5月18日 下午10:57:43 Listening Exp $
 */
public class BusinessException extends Exception {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * 错误码
     */
    private String            errorCode;

    /**
     * 错误描述
     */
    private String            errorMsg;

    public BusinessException(String errorMsg) {
        this(ExceptionCode.MESSAGE, errorMsg, null);
    }

    public BusinessException(String errorMsg, Throwable t) {
        this(t != null ? ExceptionCode.EXCEPTION : ExceptionCode.MESSAGE, errorMsg, t);
    }

    public BusinessException(ExceptionCode exceptionCode, String message) {
        this(exceptionCode, message, null);
    }

    public BusinessException(ExceptionCode exceptionCode, String errorMsg, Throwable t) {
        this(exceptionCode.getCode(), errorMsg, t);
    }

    public BusinessException(String exceptionCode, String errorMsg, Throwable e) {
        super(exceptionCode + ":" + errorMsg, e);
        this.errorCode = exceptionCode;
        this.errorMsg = errorMsg;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(String errorCode) {
        this.errorCode = errorCode;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public static BusinessException fail(String message) {
        return new BusinessException(ExceptionCode.FAIL, message);
    }

    public static BusinessException error(String message, Throwable t) {
        return new BusinessException(ExceptionCode.EXCEPTION, message, t);
    }

    public static BusinessException missing(String message) {
        return new BusinessException(ExceptionCode.PARAM_MISSING, message);
    }

    public static BusinessException message(String message) {
        return new BusinessException(ExceptionCode.MESSAGE, message);
    }
}
